package com.ckzp.jfinal.system.controller;

import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.plugins.UpImage;
import com.ckzp.jfinal.system.cache.SysDataCache;
import com.ckzp.jfinal.system.validate.SysDataValidator;
import com.ckzp.plugins.layui.LayuiData;
import com.ckzp.core.json.JsonResult;
import com.ckzp.core.model.Datas;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.system.model.SysData;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by 51594 on 2020/6/1.
 */
public class SysDataController extends BaseController {

    @Admins(roles = "administrator", name = "默认", log = false)
    public void index() {
        renderText("默认");
    }


    /**
     * 列表
     */
    @Admins(roles = "administrator", name = "数据列表", log = false)
    public void SysDataList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("SysDataList.html");
    }

    /**
     * 列表数据提供API
     */
    @Admins(roles = "administrator", name = "数据列表API", log = false)
    public void SysDataListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_data")
                .search("del_tag", 0)
                .orderby(getRequest(), "sindex");
        Page<SysData> pages = ps.paginate(SysData.dao);
        List<SysData> datalist = pages.getList();
        for (SysData data : datalist) {
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 添加
     */
    @Admins(roles = "administrator", name = "添加页面", log = false)
    public void AddSysData() {
        FormData fd = FormData.dao().setActions("ADD");
        setAttr("data", new SysData());
        setAttr("form", fd);
        render("ModifySysData.html");
    }

    /**
     * 修改字典页面
     */
    @Admins(roles = "administrator", name = "修改字典页面", log = false)
    public void EditSysData() {
        String data_key = getPara("data_key", "");
        SysData o = SysData.dao.findById(data_key);
        FormData fd = FormData.dao().setActions("EDIT");
        setAttr("form", fd);
        setAttr("data", o);
        render("ModifySysData.html");
    }

    /**
     * 修改字典Api
     */
    @Admins(roles = "administrator", name = "修改字典Api", log = false)
    @Before(SysDataValidator.class)
    public void ModifySysDataApi() {
        Datas data = getDatas();
        String data_key = data.getStr("data_key");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysData.dao.findById(data_key)
                    .set("data_title", data.getStr("data_title"))
                    .set("data_desc", data.getStr("data_desc"))
                    .set("sindex", data.getInt("sindex"))
                    .set("data_source", data.getStr("data_source"))
                    .set("data_color", data.getStr("data_color"))
                    .set("data_name", data.getStr("data_name"))
                    .set("data_filter", data.getStr("data_filter"))
                    .update();
            SysDataCache.reload();
            renderJson(Json(results, "修改"));
        } else {//添加
            boolean results = new SysData()
                    .set("data_key", data.getStr("data_key"))
                    .set("data_title", data.getStr("data_title"))
                    .set("data_desc", data.getStr("data_desc"))
                    .set("sindex", data.getInt("sindex"))
                    .set("data_source", data.getStr("data_source"))
                    .set("data_color", data.getStr("data_color"))
                    .set("data_name", data.getStr("data_name"))
                    .set("data_filter", data.getStr("data_filter"))
                    .save();
            SysDataCache.reload();
            renderJson(Json(results, "添加"));
        }
    }

    /**
     * 修改field API
     */
    @Admins(roles = "administrator", name = "修改field Ap", log = false)
    public void ModifySysDataFieldApi() {
        String data_key = getParaStr("key_id", "");
        String field = getPara("field", "");
        String value = getPara("values", "");
        SysData.dao.findById(data_key).set(field, value).update();
        renderJson(JsonResult.n(0, "修改成功"));
    }


    /**
     * 删除接口API
     */
    @Admins(roles = "administrator", name = "删除接口API", log = false)
    public void DelSysDataApi() {
        String data_key = getParaStr("data_key", "");
        SysData data = SysData.dao.findById(data_key);
        if (data == null) {
            renderJson(JsonResult.n(9, "记录不存在"));
        } else if (data.set("del_tag", 1).update()) {
            renderJson(JsonResult.n(0, "删除成功"));
        } else {
            renderJson(JsonResult.n(9, "删除失败"));
        }
    }


    /**
     * TAG
     */
    @Admins(roles = "administrator", name = "清除缓存API", log = false)
    public void ReloadSysDataApi() {
        SysDataCache.reload();
        renderJson(JsonResult.n(0, "清除缓存成功"));
    }

    /**
     * TAG
     */
    @Admins(roles = "administrator", name = "修改TAG 接口", log = false)
    public void ChangeSysDataTagApi() {
        renderJson(DoBasePub(SysData.dao, getParaStr("data_key", ""), "pub_tag", "数据字典生效"));
    }

    /**
     * 图片上传接口
     */
    @Admins(roles = "administrator", name = "图片上传接口", log = false)
    public void UploadImages() {
        renderJson(UpImage.n().upload(getRequest(), "show", true, true));
    }

}